package com.it.btyw.base

import android.content.res.ColorStateList
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.ktx.statusBarHeight
import com.it.btyw.R
import com.it.common.utils.ActivityUtils
import com.it.common.utils.hideKeyboardOnOutsideTouch
import com.it.data.manager.AppManager


/**
 *  @Description：Activity基础类
 *  @author：weishixiong
 *  @date：2023/3/28 14:25
 *
 */
abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
    protected lateinit var binding: T
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(javaClass.simpleName, "onCreate: ${javaClass.simpleName}")
        initStatus()
        setContentView()
        initObserver()
        initView()
        initData()
        initListener()
        hideKeyboardOnOutsideTouch()
        ActivityUtils.addActivity(this)


    }


    abstract fun initObserver()

    override fun onDestroy() {
        super.onDestroy()
        ActivityUtils.removeActivity(this)
    }

    override fun onResume() {
        super.onResume()
        AppManager.resumeActivity = this

    }

    abstract fun initListener()

    abstract fun initData()

    abstract fun initView()

    abstract fun setContentView()

    /**
     * 状态栏，导航栏的设置
     * @param statusBarLightFont Boolean false 文字暗色  true 文字亮色
     */
    open fun initStatus(statusBarLightFont: Boolean = false) {
        //隐藏导航栏
        supportActionBar?.hide()
        //沉侵式状态栏
        ImmersionBar.with(this).statusBarDarkFont(!statusBarLightFont).init()
        val statusBarHeight = statusBarHeight
        window.setFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        );
        Log.d(javaClass.simpleName, "initStatus:${statusBarHeight} ")


    }

    fun setTitle(
        title: Int,
        isBack: Boolean? = false,
        ivRightRes1: Int? = null,
        tvRightRes1: Int? = null,
        backAction: (() -> Unit)? = null,
        ivRight1Action: (() -> Unit)? = null,
        tvRight1Action: (() -> Unit)? = null,
        textColorRes: Int = R.color.white,
        bgColorRes: Int = R.color.white,

        ) {
        val llTitleBg = binding.root.findViewById<LinearLayout>(R.id.ll_title_bg)
        val tvTitle = binding.root.findViewById<TextView>(R.id.tv_title)
        val ivLeft1 = binding.root.findViewById<ImageView>(R.id.iv_left1)
        val ivRight1 = binding.root.findViewById<ImageView>(R.id.iv_right1)
        val tvRight1 = binding.root.findViewById<TextView>(R.id.tv_right1)

        llTitleBg.setBackgroundColor(ContextCompat.getColor(this, bgColorRes))
        tvTitle.setTextColor(ContextCompat.getColor(this, textColorRes))
        tvTitle.setText(title)


        if (isBack == true) {
            ivLeft1.visibility = View.VISIBLE
            ivLeft1.imageTintList =
                ColorStateList.valueOf(ContextCompat.getColor(this, textColorRes))
            ivLeft1.setOnClickListener {
                finish()
            }
            //
            binding.root.findViewById<View>(R.id.v_back).setOnClickListener {
                finish()
            }
        } else {
            ivLeft1.visibility = View.GONE
        }


        if (ivRightRes1 != null) {
            ivRight1.visibility = View.VISIBLE
            ivRight1.imageTintList =
                ColorStateList.valueOf(ContextCompat.getColor(this, textColorRes))
            ivRight1.setImageResource(ivRightRes1)
            ivRight1.setOnClickListener {
                ivRight1Action?.invoke()
            }
        } else {
            ivRight1.visibility = View.GONE
        }

        if (tvRightRes1 != null) {
            tvRight1.visibility = View.VISIBLE
            tvRight1.setTextColor(ContextCompat.getColor(this, textColorRes))
            tvRight1.setText(tvRightRes1)
            tvRight1.setOnClickListener {
                tvRight1Action?.invoke()
            }
        } else {
            tvRight1.visibility = View.GONE
        }


    }


}